package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Information;
import ysq.xyg.model.WebCatalog;
import ysq.xyg.model.info.InformationInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.InformationService;
import ysq.xyg.service.WebCatalogService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * 文章
 *
 */
@Controller
@RequestMapping("/information")
public class InformationController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(InformationController.class);
    @Resource
    private InformationService informationService;
    @Resource
    private WebCatalogService webCatalogService;
    private Information information;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        List<WebCatalog> list= webCatalogService.list(new WebCatalog());
        return new ModelAndView("/informationList").addObject("list", list);
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Information information, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Information> pageInfo = informationService.pageLeft(information, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/informationUpdateOrSave");
        information = informationService.selectByPrimaryKey(id);
       List<WebCatalog> list= webCatalogService.list(new WebCatalog());
        view.addObject("list", list);
        view.addObject("information", information);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(InformationInfo informationInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(informationInfo.getId())) {
            information = new Information();
            information.setId(BaseUtil.uId());
            information.setTitle(informationInfo.getTitle());
            information.setDescription(informationInfo.getDescription());
            information.setContent(informationInfo.getContent());
            information.setStatus(informationInfo.getStatus());
            information.setSort(informationInfo.getSort());
            information.setCatalogId(informationInfo.getCatalogId());
            information.setShowPic(informationInfo.getShowPic());
            information.setShowFrist(informationInfo.getShowFrist());
            information.setCreateTime(new Date());
            int id = informationService.insert(information);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            information = informationService.selectByPrimaryKey(informationInfo.getId());
            information.setTitle(informationInfo.getTitle());
            information.setContent(informationInfo.getContent());
            information.setStatus(informationInfo.getStatus());
            information.setDescription(informationInfo.getDescription());
            information.setSort(informationInfo.getSort());
            information.setCatalogId(informationInfo.getCatalogId());
            information.setShowPic(informationInfo.getShowPic());
            information.setShowFrist(informationInfo.getShowFrist());
            int id = informationService.updateByPrimaryKey(information);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = informationService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
